package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ZipUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.domain.model.LoginUser;
import com.hospital.common.exception.ServiceException;
import com.hospital.common.helper.LoginHelper;
import com.hospital.common.utils.BeanCopyUtils;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hospital.workstation.constant.DictConstants;
import com.hospital.workstation.domain.*;
import com.hospital.workstation.domain.bo.ElectronicExtendOneInfoBo;
import com.hospital.workstation.domain.bo.ElectronicExtendTwoInfoBo;
import com.hospital.workstation.domain.vo.ElectronicExtendOneInfoVo;
import com.hospital.workstation.domain.vo.ElectronicExtendTwoInfoVo;
import com.hospital.workstation.domain.vo.ElectronicManagerInfoVo;
import com.hospital.workstation.domain.vo.json.BasicMonitorOneVo;
import com.hospital.workstation.domain.vo.json.CombindMedicalVo;
import com.hospital.workstation.domain.vo.json.DailyMedicalVo;
import com.hospital.workstation.domain.vo.json.ExportMonitorJsonVo;
import com.hospital.workstation.mapper.*;
import com.hospital.workstation.utils.PdfUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.ElectronicMonitorOneInfoBo;
import com.hospital.workstation.domain.vo.ElectronicMonitorOneInfoVo;
import com.hospital.workstation.service.IElectronicMonitorOneInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialException;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * A表 监测信息Service业务层处理
 *
 * @author whb
 * @date 2024-05-07
 */
@RequiredArgsConstructor
@Service
public class ElectronicMonitorOneInfoServiceImpl implements IElectronicMonitorOneInfoService {

    private final ElectronicMonitorOneInfoMapper baseMapper;
    private final ElectronicManagerInfoMapper managerInfoMapper;
    private final PatientBaseInfoMapper patientBaseInfoMapper;
    private final ElectronicExtendOneInfoMapper extendOneInfoMapper;
    private final ElectronicExtendTwoInfoMapper extendTwoInfoMapper;
    private final HisAdmittingDiagInfoMapper diagInfoMapper;

    /**
     * 查询A表 监测信息
     */
    @Override
    public ElectronicMonitorOneInfoVo queryById(Long id) {
        ElectronicMonitorOneInfoVo electronicMonitorOneInfoVo = baseMapper.selectVoById(id);
        ElectronicManagerInfo electronicManagerInfo = managerInfoMapper.selectOne(new LambdaQueryWrapper<ElectronicManagerInfo>()
            .eq(ElectronicManagerInfo::getMonitorId, electronicMonitorOneInfoVo.getId()));
        electronicMonitorOneInfoVo.setMedicalRecordNo(electronicManagerInfo.getMedicalRecordNo());
        electronicMonitorOneInfoVo.setManageId(electronicManagerInfo.getId());
        // 查询续一表
        List<ElectronicExtendOneInfoVo> extendOneInfoVoList = extendOneInfoMapper.selectVoList(new LambdaQueryWrapper<ElectronicExtendOneInfo>()
            .eq(ElectronicExtendOneInfo::getMonitorId, electronicMonitorOneInfoVo.getId()));
        electronicMonitorOneInfoVo.setElectronicExtendOneInfoVoList(extendOneInfoVoList);
        // 查询续二表
        List<ElectronicExtendTwoInfoVo> extendTwoInfoVoList = extendTwoInfoMapper.selectVoList(new LambdaQueryWrapper<ElectronicExtendTwoInfo>()
            .eq(ElectronicExtendTwoInfo::getMonitorId, electronicMonitorOneInfoVo.getId()));
        electronicMonitorOneInfoVo.setElectronicExtendTwoInfoVoList(extendTwoInfoVoList);
        return electronicMonitorOneInfoVo;
    }

    /**
     * 查询A表 监测信息列表
     */
    @Override
    public TableDataInfo<ElectronicMonitorOneInfoVo> queryPageList(ElectronicMonitorOneInfoBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (ObjectUtil.isNull(loginUser)) {
            throw new ServiceException("请先登录!");
        }
        LambdaQueryWrapper<ElectronicMonitorOneInfo> lqw = buildQueryWrapper(bo);
        Set<String> rolePermission = loginUser.getRolePermission();
        if (rolePermission.contains("monitor")) {
            lqw.eq(ElectronicMonitorOneInfo::getCreateBy, loginUser.getUsername());
        } else if (rolePermission.contains("quality2")) {
            lqw.eq(ElectronicMonitorOneInfo::getSubmitStatus, "1");
        } else {
        }
        lqw.orderByDesc(ElectronicMonitorOneInfo::getMonitorRecordNo)
            .orderByDesc(ElectronicMonitorOneInfo::getSubmitStatus)
            .orderByDesc(ElectronicMonitorOneInfo::getCreateTime)
            .select(ElectronicMonitorOneInfo::getId, ElectronicMonitorOneInfo::getMonitorRecordNo, ElectronicMonitorOneInfo::getPatientNo,
                ElectronicMonitorOneInfo::getPatientNameAcronym, ElectronicMonitorOneInfo::getInputFormDate, ElectronicMonitorOneInfo::getAuditStatus,
                ElectronicMonitorOneInfo::getSubmitStatus, ElectronicMonitorOneInfo::getExportStatus,
                ElectronicMonitorOneInfo::getInspectorSign, ElectronicMonitorOneInfo::getQualityControllerSign);
        Page<ElectronicMonitorOneInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询A表 监测信息列表
     */
    @Override
    public List<ElectronicMonitorOneInfoVo> queryList(ElectronicMonitorOneInfoBo bo) {
        LambdaQueryWrapper<ElectronicMonitorOneInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ElectronicMonitorOneInfo> buildQueryWrapper(ElectronicMonitorOneInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ElectronicMonitorOneInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMonitorRecordNo() != null, ElectronicMonitorOneInfo::getMonitorRecordNo, bo.getMonitorRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getHospitalNo()), ElectronicMonitorOneInfo::getHospitalNo, bo.getHospitalNo());
        lqw.like(StringUtils.isNotBlank(bo.getHospitalName()), ElectronicMonitorOneInfo::getHospitalName, bo.getHospitalName());
        lqw.eq(StringUtils.isNotBlank(bo.getBranchCenter()), ElectronicMonitorOneInfo::getBranchCenter, bo.getBranchCenter());
        lqw.eq(bo.getDeptId() != null, ElectronicMonitorOneInfo::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getBedNo()), ElectronicMonitorOneInfo::getBedNo, bo.getBedNo());
        lqw.like(StringUtils.isNotBlank(bo.getPatientName()), ElectronicMonitorOneInfo::getPatientName, bo.getPatientName());
        lqw.like(StringUtils.isNotBlank(bo.getPatientNameAcronym()), ElectronicMonitorOneInfo::getPatientNameAcronym, bo.getPatientNameAcronym());
        lqw.eq(StringUtils.isNotBlank(bo.getPatientNo()), ElectronicMonitorOneInfo::getPatientNo, bo.getPatientNo());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null, ElectronicMonitorOneInfo::getInputFormDate,
            params.get("beginTime") + " 00:00:00", params.get("endTime") + " 23:59:59");
        lqw.eq(StringUtils.isNotBlank(bo.getWeather()), ElectronicMonitorOneInfo::getWeather, bo.getWeather());
        lqw.eq(StringUtils.isNotBlank(bo.getTemperature()), ElectronicMonitorOneInfo::getTemperature, bo.getTemperature());
        lqw.eq(StringUtils.isNotBlank(bo.getPatientSex()), ElectronicMonitorOneInfo::getPatientSex, bo.getPatientSex());
        lqw.eq(bo.getBirthdate() != null, ElectronicMonitorOneInfo::getBirthdate, bo.getBirthdate());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoneNo()), ElectronicMonitorOneInfo::getPhoneNo, bo.getPhoneNo());
        lqw.eq(bo.getPatientHeight() != null, ElectronicMonitorOneInfo::getPatientHeight, bo.getPatientHeight());
        lqw.eq(bo.getPatientWeight() != null, ElectronicMonitorOneInfo::getPatientWeight, bo.getPatientWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getNation()), ElectronicMonitorOneInfo::getNation, bo.getNation());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHaveAllergy()), ElectronicMonitorOneInfo::getIsHaveAllergy, bo.getIsHaveAllergy());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergySource()), ElectronicMonitorOneInfo::getAllergySource, bo.getAllergySource());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergyFood()), ElectronicMonitorOneInfo::getAllergyFood, bo.getAllergyFood());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergyDrug()), ElectronicMonitorOneInfo::getAllergyDrug, bo.getAllergyDrug());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergyUnknown()), ElectronicMonitorOneInfo::getAllergyUnknown, bo.getAllergyUnknown());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergyOther()), ElectronicMonitorOneInfo::getAllergyOther, bo.getAllergyOther());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergyShow()), ElectronicMonitorOneInfo::getAllergyShow, bo.getAllergyShow());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergyShowOther()), ElectronicMonitorOneInfo::getAllergyShowOther, bo.getAllergyShowOther());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHaveIllness()), ElectronicMonitorOneInfo::getIsHaveIllness, bo.getIsHaveIllness());
        lqw.eq(StringUtils.isNotBlank(bo.getIllnessShow()), ElectronicMonitorOneInfo::getIllnessShow, bo.getIllnessShow());
        lqw.eq(StringUtils.isNotBlank(bo.getIllnessShowOther()), ElectronicMonitorOneInfo::getIllnessShowOther, bo.getIllnessShowOther());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHaveFamily()), ElectronicMonitorOneInfo::getIsHaveFamily, bo.getIsHaveFamily());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationParent()), ElectronicMonitorOneInfo::getRelationParent, bo.getRelationParent());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationBrotherSister()), ElectronicMonitorOneInfo::getRelationBrotherSister, bo.getRelationBrotherSister());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationPills()), ElectronicMonitorOneInfo::getRelationPills, bo.getRelationPills());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationPillsHave()), ElectronicMonitorOneInfo::getRelationPillsHave, bo.getRelationPillsHave());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationAllergyOther()), ElectronicMonitorOneInfo::getRelationAllergyOther, bo.getRelationAllergyOther());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationAllergyOtherHave()), ElectronicMonitorOneInfo::getRelationAllergyOtherHave, bo.getRelationAllergyOtherHave());
        lqw.eq(StringUtils.isNotBlank(bo.getWesternMedicineDiagnose()), ElectronicMonitorOneInfo::getWesternMedicineDiagnose, bo.getWesternMedicineDiagnose());
        lqw.eq(StringUtils.isNotBlank(bo.getWesternMedicineDiagnoseOther()), ElectronicMonitorOneInfo::getWesternMedicineDiagnoseOther, bo.getWesternMedicineDiagnoseOther());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHaveTcmDiagnose()), ElectronicMonitorOneInfo::getIsHaveTcmDiagnose, bo.getIsHaveTcmDiagnose());
        lqw.eq(StringUtils.isNotBlank(bo.getTcmDiagnose()), ElectronicMonitorOneInfo::getTcmDiagnose, bo.getTcmDiagnose());
        lqw.eq(StringUtils.isNotBlank(bo.getTcmDiagnoseOther()), ElectronicMonitorOneInfo::getTcmDiagnoseOther, bo.getTcmDiagnoseOther());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHavaDialectical()), ElectronicMonitorOneInfo::getIsHavaDialectical, bo.getIsHavaDialectical());
        lqw.eq(StringUtils.isNotBlank(bo.getDialecticalDetail()), ElectronicMonitorOneInfo::getDialecticalDetail, bo.getDialecticalDetail());
        lqw.eq(StringUtils.isNotBlank(bo.getWesternAdmittingDiagnose()), ElectronicMonitorOneInfo::getWesternAdmittingDiagnose, bo.getWesternAdmittingDiagnose());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHaveTcmAdmittingDiagnose()), ElectronicMonitorOneInfo::getIsHaveTcmAdmittingDiagnose, bo.getIsHaveTcmAdmittingDiagnose());
        lqw.eq(StringUtils.isNotBlank(bo.getTcmAdmittingDiagnose()), ElectronicMonitorOneInfo::getTcmAdmittingDiagnose, bo.getTcmAdmittingDiagnose());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHaveAdmittingDialectical()), ElectronicMonitorOneInfo::getIsHaveAdmittingDialectical, bo.getIsHaveAdmittingDialectical());
        lqw.eq(StringUtils.isNotBlank(bo.getAdmittingDialecticalDetail()), ElectronicMonitorOneInfo::getAdmittingDialecticalDetail, bo.getAdmittingDialecticalDetail());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHavaUse()), ElectronicMonitorOneInfo::getIsHavaUse, bo.getIsHavaUse());
        lqw.eq(bo.getUseDays() != null, ElectronicMonitorOneInfo::getUseDays, bo.getUseDays());
        lqw.eq(bo.getFirstUseDate() != null, ElectronicMonitorOneInfo::getFirstUseDate, bo.getFirstUseDate());
        lqw.eq(bo.getLastUseDate() != null, ElectronicMonitorOneInfo::getLastUseDate, bo.getLastUseDate());
        lqw.eq(StringUtils.isNotBlank(bo.getMenstruumType()), ElectronicMonitorOneInfo::getMenstruumType, bo.getMenstruumType());
        lqw.eq(StringUtils.isNotBlank(bo.getGivePillsPathway()), ElectronicMonitorOneInfo::getGivePillsPathway, bo.getGivePillsPathway());
        lqw.eq(StringUtils.isNotBlank(bo.getIsHaveBad()), ElectronicMonitorOneInfo::getIsHaveBad, bo.getIsHaveBad());
        lqw.likeRight(StringUtils.isNotBlank(bo.getInspectorSign()), ElectronicMonitorOneInfo::getInspectorSign, bo.getInspectorSign());
        lqw.eq(bo.getInspectorDate() != null, ElectronicMonitorOneInfo::getInspectorDate, bo.getInspectorDate());
        lqw.likeRight(StringUtils.isNotBlank(bo.getQualityControllerSign()), ElectronicMonitorOneInfo::getQualityControllerSign, bo.getQualityControllerSign());
        lqw.eq(bo.getQualityControllerDate() != null, ElectronicMonitorOneInfo::getQualityControllerDate, bo.getQualityControllerDate());
        lqw.eq(StringUtils.isNotBlank(bo.getSubmitStatus()), ElectronicMonitorOneInfo::getSubmitStatus, bo.getSubmitStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), ElectronicMonitorOneInfo::getAuditStatus, bo.getAuditStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getExportStatus()), ElectronicMonitorOneInfo::getExportStatus, bo.getExportStatus());
        return lqw;
    }

    /**
     * 保存A表 监测信息
     */
    @Transactional
    @Override
    public R<Void> insertByBo(ElectronicMonitorOneInfoBo bo) {
        // 1.保存A表监测信息
        ElectronicMonitorOneInfo electronicMonitorOneInfo = BeanUtil.toBean(bo, ElectronicMonitorOneInfo.class);
        validEntityBeforeSave(electronicMonitorOneInfo);
        boolean flag = baseMapper.insertOrUpdate(electronicMonitorOneInfo);
        if (!flag) {
            throw new ServiceException("保存A表监测信息异常");
        }
        // 2.保存续一表
        try {
            extendOneInfoMapper.delete(new LambdaQueryWrapper<ElectronicExtendOneInfo>()
                .eq(ElectronicExtendOneInfo::getMonitorId, electronicMonitorOneInfo.getId()));
        } catch (Exception e) {
            throw new ServiceException("删除续一表异常");
        }
        List<ElectronicExtendOneInfoBo> electronicExtendOneInfoBoList = bo.getElectronicExtendOneInfoBoList();
        if (ObjectUtil.isNotNull(electronicExtendOneInfoBoList) && electronicExtendOneInfoBoList.size() > 0) {
            List<ElectronicExtendOneInfo> electronicExtendOneInfoList = BeanCopyUtils.copyList(electronicExtendOneInfoBoList, ElectronicExtendOneInfo.class);
            electronicExtendOneInfoList.stream().forEach(electronicExtendOneInfo -> electronicExtendOneInfo.setMonitorId(electronicMonitorOneInfo.getId()));
            flag = extendOneInfoMapper.insertBatch(electronicExtendOneInfoList);
            if (!flag) {
                throw new ServiceException("保存续一表异常");
            }
        }
        // 2.保存续二表
        try {
            extendTwoInfoMapper.delete(new LambdaQueryWrapper<ElectronicExtendTwoInfo>()
                .eq(ElectronicExtendTwoInfo::getMonitorId, electronicMonitorOneInfo.getId()));
        } catch (Exception e) {
            throw new ServiceException("删除续二表异常");
        }
        List<ElectronicExtendTwoInfoBo> electronicExtendTwoInfoBoList = bo.getElectronicExtendTwoInfoBoList();
        if (ObjectUtil.isNotNull(electronicExtendTwoInfoBoList) && electronicExtendTwoInfoBoList.size() > 0) {
            List<ElectronicExtendTwoInfo> electronicExtendTwoInfoList = BeanCopyUtils.copyList(electronicExtendTwoInfoBoList, ElectronicExtendTwoInfo.class);
            electronicExtendTwoInfoList.stream().forEach(electronicExtendTwoInfo -> electronicExtendTwoInfo.setMonitorId(electronicMonitorOneInfo.getId()));
            flag = extendTwoInfoMapper.insertBatch(electronicExtendTwoInfoList);
            if (!flag) {
                throw new ServiceException("保存续二表异常");
            }
        }
        // 4.新增或更新监测管理信息
        Long manageId = bo.getManageId();
        ElectronicManagerInfo electronicManagerInfo = new ElectronicManagerInfo();
        // 监测管理主键id
        if (ObjectUtil.isNotNull(manageId)) {
            electronicManagerInfo.setId(manageId);
        }
        // 患者流水号
        Integer medicalRecordNo = bo.getMedicalRecordNo();
        if (ObjectUtil.isNull(medicalRecordNo)) {
            throw new ServiceException("患者流水号不能为空");
        }
        electronicManagerInfo.setMedicalRecordNo(medicalRecordNo);
        // 监测表id
        electronicManagerInfo.setMonitorId(electronicMonitorOneInfo.getId());
        // 监测表流水号
        electronicManagerInfo.setMonitorRecordNo(electronicMonitorOneInfo.getMonitorRecordNo());
        // 表类型
        electronicManagerInfo.setMonitorTableType(DictConstants.MONITOR_TABLE_TYPE_A);
        // 入组日期
        electronicManagerInfo.setInputFormDate(electronicMonitorOneInfo.getInputFormDate());
        // 是否已入组（0未入组 1已入组）
        electronicManagerInfo.setIsInputForm(DictConstants.IS_INPUT_FORM_Y);
        flag = managerInfoMapper.insertOrUpdate(electronicManagerInfo);
        if (!flag) {
            throw new ServiceException("新增或更新监测管理信息异常");
        }
        // 5.修改基本患者信息
        PatientBaseInfo patientBaseInfo = patientBaseInfoMapper.selectOne(new LambdaQueryWrapper<PatientBaseInfo>()
            .eq(PatientBaseInfo::getMedicalRecordNo, bo.getMedicalRecordNo()));
        if ("1".equals(bo.getSubmitStatus())) {
            patientBaseInfo.setIsHaveMonitor("1");
        }
        flag = patientBaseInfoMapper.updateById(patientBaseInfo) > 0;
        if (!flag) {
            throw new ServiceException("修改患者信息失败");
        }
        return R.ok("保存成功");
    }

    /**
     * 修改A表 监测信息
     */
    @Override
    public Boolean updateByBo(ElectronicMonitorOneInfoBo bo) {
        ElectronicMonitorOneInfo update = BeanUtil.toBean(bo, ElectronicMonitorOneInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ElectronicMonitorOneInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除A表 监测信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        try {
            // 1.删除管理信息表数据
            managerInfoMapper.delete(new LambdaQueryWrapper<ElectronicManagerInfo>()
                .eq(ElectronicManagerInfo::getMonitorId, ids.toArray()[0])
                .eq(ElectronicManagerInfo::getMonitorTableType, "0"));
            // 2.删除A表数据
            return baseMapper.deleteBatchIds(ids) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("删除异常");
        }
    }

    /**
     * 根据患者流水号查询监测信息
     *
     * @param medicalRecordNo 患者流水号
     * @return
     */
    @Override
    public R<ElectronicMonitorOneInfoVo> queryMonitorOneInfo(Integer medicalRecordNo) {
        ElectronicMonitorOneInfoVo electronicMonitorOneInfoVo = new ElectronicMonitorOneInfoVo();
        // 1.查询患者基本信息
        PatientBaseInfo patientBaseInfo = patientBaseInfoMapper.selectOne(new LambdaQueryWrapper<PatientBaseInfo>()
            .eq(PatientBaseInfo::getMedicalRecordNo, medicalRecordNo));
        if (ObjectUtil.isNull(patientBaseInfo)) {
            throw new ServiceException("患者已不存在");
        } else {
            // 患者姓名缩写
            String chinesePerLetterToUpper = StringUtils.getChinesePerLetterToUpper(patientBaseInfo.getPatientName());
            electronicMonitorOneInfoVo.setPatientNameAcronym(StringUtils.retainDefaultLength(4, chinesePerLetterToUpper, "*"));
            // 住院号
            electronicMonitorOneInfoVo.setPatientNo(StringUtils.padl(patientBaseInfo.getMedicareNo(), 10, 'A'));
            // 性别
            electronicMonitorOneInfoVo.setPatientSex(patientBaseInfo.getPatientSex());
            // 出生日期
            electronicMonitorOneInfoVo.setBirthdate(patientBaseInfo.getBirthdate());
            // 联系电话
            electronicMonitorOneInfoVo.setPhoneNo(patientBaseInfo.getPhoneNo());
            // 身高
            electronicMonitorOneInfoVo.setPatientHeight(patientBaseInfo.getPatientHeight());
            // 体重
            electronicMonitorOneInfoVo.setPatientWeight(patientBaseInfo.getPatientWeight());
            // 民族
            electronicMonitorOneInfoVo.setNation(patientBaseInfo.getNation());
            // 入院诊断
            Map diagMap = queryDiagInfo(medicalRecordNo);
            electronicMonitorOneInfoVo.setWesternAdmittingDiagnose((String) diagMap.get("west"));
            String traditionalDiagInfo = (String) diagMap.get("traditional");
            String syndromeDiagInfo = (String) diagMap.get("syndrome");
            if (ObjectUtil.isNotNull(traditionalDiagInfo)) {
                electronicMonitorOneInfoVo.setIsHaveTcmAdmittingDiagnose("1");
                electronicMonitorOneInfoVo.setTcmAdmittingDiagnose(traditionalDiagInfo);
            } else {
                electronicMonitorOneInfoVo.setIsHaveTcmAdmittingDiagnose("0");
            }
            if (ObjectUtil.isNotNull(syndromeDiagInfo)) {
                electronicMonitorOneInfoVo.setIsHaveAdmittingDialectical("1");
                electronicMonitorOneInfoVo.setAdmittingDialecticalDetail(syndromeDiagInfo);
            } else {
                electronicMonitorOneInfoVo.setIsHaveAdmittingDialectical("0");
            }
        }
        // 2.查询未入组的监测信息
        ElectronicManagerInfoVo electronicManagerInfoVo = managerInfoMapper.selectVoOne(new LambdaQueryWrapper<ElectronicManagerInfo>()
            .eq(ElectronicManagerInfo::getMedicalRecordNo, medicalRecordNo)
            .eq(ElectronicManagerInfo::getMonitorTableType, DictConstants.MONITOR_TABLE_TYPE_A)
            .eq(ElectronicManagerInfo::getIsInputForm, DictConstants.IS_INPUT_FORM_N));
        if (ObjectUtil.isNotNull(electronicManagerInfoVo)) {
            electronicMonitorOneInfoVo.setManageId(electronicManagerInfoVo.getId());
            return R.ok(electronicMonitorOneInfoVo);
        }
        // 3.查询最近入组的监测信息
        electronicManagerInfoVo = managerInfoMapper.selectVoOne(new LambdaQueryWrapper<ElectronicManagerInfo>()
            .eq(ElectronicManagerInfo::getMedicalRecordNo, medicalRecordNo)
            .eq(ElectronicManagerInfo::getMonitorTableType, DictConstants.MONITOR_TABLE_TYPE_A)
            .eq(ElectronicManagerInfo::getIsInputForm, DictConstants.IS_INPUT_FORM_Y)
            .orderByDesc(ElectronicManagerInfo::getUpdateTime)
            .last("limit 1"));
        if (ObjectUtil.isNotNull(electronicManagerInfoVo)) {
            // 3.1 根据监测表id查询监测A表信息
            Long monitorId = electronicManagerInfoVo.getMonitorId();
            if (ObjectUtil.isNull(monitorId)) {
                throw new ServiceException("没有查询到监测表id");
            }
            electronicMonitorOneInfoVo = baseMapper.selectVoById(monitorId);
            if (ObjectUtil.isNull(electronicMonitorOneInfoVo)) {
                // 该监测A表信息已经不存在
                managerInfoMapper.deleteById(electronicManagerInfoVo.getId());
                return R.ok(electronicMonitorOneInfoVo);
            }
            // 3.2 该监测A表信息存在
            electronicMonitorOneInfoVo.setManageId(electronicManagerInfoVo.getId());
            // 查询续一表
            List<ElectronicExtendOneInfoVo> electronicExtendOneInfoVoList = extendOneInfoMapper.selectVoList(new LambdaQueryWrapper<ElectronicExtendOneInfo>()
                .eq(ElectronicExtendOneInfo::getMonitorId, monitorId));
            electronicMonitorOneInfoVo.setElectronicExtendOneInfoVoList(electronicExtendOneInfoVoList);
            // 查询续二表
            List<ElectronicExtendTwoInfoVo> electronicExtendTwoInfoVoList = extendTwoInfoMapper.selectVoList(new LambdaQueryWrapper<ElectronicExtendTwoInfo>()
                .eq(ElectronicExtendTwoInfo::getMonitorId, monitorId));
            electronicMonitorOneInfoVo.setElectronicExtendTwoInfoVoList(electronicExtendTwoInfoVoList);
            return R.ok(electronicMonitorOneInfoVo);
        }
        return R.ok(electronicMonitorOneInfoVo);
    }

    /**
     * 检测表流水号是否存在
     *
     * @param monitorRecordNo 检测表流水号
     * @return
     */
    @Override
    public R<Boolean> isExistMonitorRecordNo(Integer monitorRecordNo) {
        List<ElectronicMonitorOneInfoVo> electronicMonitorOneInfoVoList = baseMapper.selectVoList(new LambdaQueryWrapper<ElectronicMonitorOneInfo>()
            .eq(ElectronicMonitorOneInfo::getMonitorRecordNo, monitorRecordNo));
        return electronicMonitorOneInfoVoList.size() > 0 ? R.ok("已存在", false) : R.ok("不存在", true);
    }

    /**
     * 查询患者的诊断信息
     *
     * @param medicalRecordNo 患者流水号
     * @return
     */
    @Override
    public Map queryDiagInfo(Integer medicalRecordNo) {
        HashMap<String, String> diagMap = new HashMap<>();
        // 查询入院诊断信息
        List<HisAdmittingDiagInfo> hisAdmittingDiagInfoList = diagInfoMapper.selectList(new LambdaQueryWrapper<HisAdmittingDiagInfo>()
            .eq(HisAdmittingDiagInfo::getMedicalRecordNo, medicalRecordNo));
        List<String> admittingDiagnosisWesternList = hisAdmittingDiagInfoList.stream().filter(hisAdmittingDiagInfo -> StringUtils.equals(hisAdmittingDiagInfo.getDiagCat(), "西医"))
            .map(HisAdmittingDiagInfo::getDiagDesc)
            .distinct()
            .collect(Collectors.toList());
        List<String> admittingDiagnosisTraditionalList = hisAdmittingDiagInfoList.stream().filter(hisAdmittingDiagInfo -> StringUtils.equals(hisAdmittingDiagInfo.getDiagCat(), "中医"))
            .map(HisAdmittingDiagInfo::getDiagDesc)
            .distinct()
            .collect(Collectors.toList());
        List<String> admittingDiagnosisSyndromeList = hisAdmittingDiagInfoList.stream().filter(hisAdmittingDiagInfo -> StringUtils.equals(hisAdmittingDiagInfo.getDiagCat(), "证型"))
            .map(HisAdmittingDiagInfo::getDiagDesc)
            .distinct()
            .collect(Collectors.toList());
        // 西医诊断信息
        if (admittingDiagnosisWesternList.size() > 0) {
            String west = String.join("；", admittingDiagnosisWesternList);
            diagMap.put("west", west);
        }
        // 中医诊断
        if (admittingDiagnosisTraditionalList.size() > 0) {
            String traditional = String.join("；", admittingDiagnosisTraditionalList);
            diagMap.put("traditional", traditional);
        }
        // 证型
        if (admittingDiagnosisSyndromeList.size() > 0) {
            String syndrome = String.join("；", admittingDiagnosisSyndromeList);
            diagMap.put("syndrome", syndrome);
        }
        return diagMap;
    }

    /**
     * 驳回
     *
     * @param bo
     * @return
     */
    @Override
    public R<Void> reject(ElectronicMonitorOneInfoBo bo) {
        Long id = bo.getId();
        if (ObjectUtil.isNull(id)) {
            throw new ServiceException("id不能为空");
        }
        ElectronicMonitorOneInfo electronicMonitorOneInfo = baseMapper.selectById(id);
        electronicMonitorOneInfo.setMonitorRecordNo(bo.getMonitorRecordNo());
        electronicMonitorOneInfo.setRejectReason(bo.getRejectReason());
        electronicMonitorOneInfo.setSubmitStatus("0");
        electronicMonitorOneInfo.setAuditStatus("0");
        boolean flag = baseMapper.updateById(electronicMonitorOneInfo) > 0;
        if (!flag) {
            return R.fail("驳回失败");
        }
        return R.ok("驳回成功");
    }


    @Override
    public void exportFile(HttpServletResponse response, Long[] ids) throws IOException {
        String zipName = "舒血宁注射液监测表（A）.zip";
        /*response.setContentType("application/octet-stream; charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="+zipName+"");*/
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(outputStream);
        for (int i = 0; i < ids.length; i++) {
            long id = ids[i];
            ElectronicMonitorOneInfoVo electronicMonitorOneInfoVo = baseMapper.selectVoById(id);
            List<ElectronicExtendOneInfoVo> electronicExtendOneInfoVoList = extendOneInfoMapper.selectVoList(new LambdaQueryWrapper<ElectronicExtendOneInfo>()
                .eq(ElectronicExtendOneInfo::getMonitorId, electronicMonitorOneInfoVo.getId()));

            List<ElectronicExtendTwoInfoVo> electronicExtendTwoInfoVoList = extendTwoInfoMapper.selectVoList(new LambdaQueryWrapper<ElectronicExtendTwoInfo>()
                .eq(ElectronicExtendTwoInfo::getMonitorId, electronicMonitorOneInfoVo.getId()));
            byte[] export = export(electronicMonitorOneInfoVo, electronicExtendOneInfoVoList, electronicExtendTwoInfoVoList);
            byte[] bytes = PdfUtil.word2pdf(export);
            String name = "舒血宁注射液监测表（A）_" + electronicMonitorOneInfoVo.getMonitorRecordNo() + ".pdf";

            ZipEntry entry = new ZipEntry(name); // 设置zip文件中的文件名
            zipOut.putNextEntry(entry);
            zipOut.write(bytes);
            zipOut.closeEntry();
        }
// 关闭zip输出流
        zipOut.close();
        IOUtils.closeQuietly(zipOut);
        byte[] data = outputStream.toByteArray();
        genCode(response, data, zipName);
    }


    private void genCode(HttpServletResponse response, byte[] data, String zipName) throws IOException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("A表监测信息", "UTF-8") + ".zip");
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }


    public byte[] export(ElectronicMonitorOneInfoVo electronicMonitorOneInfoVo,
                         List<ElectronicExtendOneInfoVo> extendOneList,
                         List<ElectronicExtendTwoInfoVo> extendTwoList) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            Map<String, Object> datas = buildDataMap(electronicMonitorOneInfoVo, extendOneList, extendTwoList);

            // 创建循环策略和配置
            LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
            Configure config = Configure.builder()
                .bind("extendOneInfo", policy)
                .bind("extendTwoInfo", policy)
                .build();

            ResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource resource = resourceLoader.getResource("classpath:template/A.docx");
            InputStream inputStream = resource.getInputStream();
            XWPFTemplate template = XWPFTemplate.compile(inputStream, config).render(datas);
            template.write(out);
            return out.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("导出失败");
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public Map<String, Object> buildDataMap(ElectronicMonitorOneInfoVo vo,
                                            List<ElectronicExtendOneInfoVo> extendOneList,
                                            List<ElectronicExtendTwoInfoVo> extendTwoList) {
        Map<String, Object> datas = new HashMap<>();
        datas.put("monitorRecordNo", vo.getMonitorRecordNo());
        datas.put("hospitalNo", vo.getHospitalNo());
        datas.put("patientNo", vo.getPatientNo());
        datas.put("patientNameAcronym", vo.getPatientNameAcronym());
        Date inputFormDate = vo.getInputFormDate();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy /MM /dd");
        datas.put("inputFormDate", dateFormat.format(inputFormDate));
        datas.put("branchCenter", vo.getBranchCenter());
        datas.put("hospitalName", vo.getHospitalName());
        datas.put("weather", vo.getWeather());
        datas.put("temperature", vo.getTemperature());
        String patientSex = vo.getPatientSex();
        Map<String, Object> patientSex1 = getData0("patientSex", patientSex);
        datas.putAll(patientSex1);
        Date birthdate = vo.getBirthdate();
        dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        datas.put("birthdate", dateFormat.format(birthdate));
        datas.put("phoneNo", vo.getPhoneNo());
        datas.put("patientHeight", String.valueOf(vo.getPatientHeight()));
        datas.put("patientWeight", String.valueOf(vo.getPatientWeight()));
        String nation = vo.getNation();
        Map<String, Object> nation1 = getData0("nation", nation.contains("汉") ? "0" : "1");
        datas.putAll(nation1);
        if (!nation.contains("汉")) {
            //if (!"1".equals(nation) && !"2".equals(nation)) {
            datas.put("nation2", nation);
        }

        String isHaveAllergy = vo.getIsHaveAllergy();
        Map<String, Object> isHaveAllergy1 = getData0("isHaveAllergy", isHaveAllergy);
        datas.putAll(isHaveAllergy1);

        String allergySource = vo.getAllergySource();
        Map<String, Object> allergySource1 = getData1("allergySource", allergySource);
        datas.putAll(allergySource1);

        datas.put("allergyFood", vo.getAllergyFood());
        datas.put("allergyDrug", vo.getAllergyDrug());
        datas.put("allergyOther", vo.getAllergyOther());

        String allergyShow = vo.getAllergyShow();
        Map<String, Object> allergyShow1 = getData1("allergyShow", allergyShow);
        datas.putAll(allergyShow1);

        datas.put("allergyShowOther", vo.getAllergyShowOther());

        String isHaveIllness = vo.getIsHaveIllness();
        Map<String, Object> isHaveIllness1 = getData0("isHaveIllness", isHaveIllness);
        datas.putAll(isHaveIllness1);

        String illnessShow = vo.getIllnessShow();
        Map<String, Object> illnessShow1 = getData1("illnessShow", illnessShow);
        datas.putAll(illnessShow1);

        datas.put("illnessShowOther", vo.getIllnessShowOther());

        String isHaveFamily = vo.getIsHaveFamily();
        Map<String, Object> isHaveFamily1 = getData0("isHaveFamily", isHaveFamily);
        datas.putAll(isHaveFamily1);

        String relationParent = vo.getRelationParent();
        Map<String, Object> relationParent1 = getData0("relationParent", relationParent);
        datas.putAll(relationParent1);

        String relationBrotherSister = vo.getRelationBrotherSister();
        Map<String, Object> relationBrotherSister1 = getData0("relationBrotherSister", relationBrotherSister);
        datas.putAll(relationBrotherSister1);

        String relationPills = vo.getRelationPills();
        Map<String, Object> relationPills1 = getData0("relationPills", relationPills);
        datas.putAll(relationPills1);

        datas.put("relationPillsHave", vo.getRelationPillsHave());

        String relationAllergyOther = vo.getRelationAllergyOther();
        Map<String, Object> relationAllergyOther1 = getData0("relationAllergyOther", relationAllergyOther);
        datas.putAll(relationAllergyOther1);

        datas.put("relationAllergyOtherHave", vo.getRelationAllergyOtherHave());

        String westernMedicineDiagnose = vo.getWesternMedicineDiagnose();
        Map<String, Object> westernMedicineDiagnose1 = getData1("westernMedicineDiagnose", westernMedicineDiagnose);
        datas.putAll(westernMedicineDiagnose1);

        datas.put("westernMedicineDiagnoseOther", vo.getWesternMedicineDiagnoseOther());

        String isHaveTcmDiagnose = vo.getIsHaveTcmDiagnose();
        Map<String, Object> isHaveTcmDiagnose1 = getData0("isHaveTcmDiagnose", isHaveTcmDiagnose);
        datas.putAll(isHaveTcmDiagnose1);

        String tcmDiagnose = vo.getTcmDiagnose();
        Map<String, Object> tcmDiagnose1 = getData1("tcmDiagnose", tcmDiagnose);
        datas.putAll(tcmDiagnose1);

        datas.put("tcmDiagnoseOther", vo.getTcmDiagnoseOther());

        String isHavaDialectical = vo.getIsHavaDialectical();
        Map<String, Object> isHavaDialectical1 = getData0("isHavaDialectical", isHavaDialectical);
        datas.putAll(isHavaDialectical1);

        datas.put("dialecticalDetail", vo.getDialecticalDetail());

        String westernAdmittingDiagnose = vo.getWesternAdmittingDiagnose();
        Map<String, Object> westernAdmittingDiagnose1 = getData2("westernAdmittingDiagnose", westernAdmittingDiagnose);
        datas.putAll(westernAdmittingDiagnose1);

        String isHaveTcmAdmittingDiagnose = vo.getIsHaveTcmAdmittingDiagnose();
        Map<String, Object> isHaveTcmAdmittingDiagnose1 = getData0("isHaveTcmAdmittingDiagnose", isHaveTcmAdmittingDiagnose);
        datas.putAll(isHaveTcmAdmittingDiagnose1);
        String tcmAdmittingDiagnose = vo.getTcmAdmittingDiagnose();
        if (StringUtils.isNotBlank(tcmAdmittingDiagnose)){
            Map<String, Object> tcmAdmittingDiagnose1 = getData2("tcmAdmittingDiagnose", tcmAdmittingDiagnose.trim());
            //tcmAdmittingDiagnose1.putAll(datas);
            datas.putAll(tcmAdmittingDiagnose1);
        }
        String isHaveAdmittingDialectical = vo.getIsHaveAdmittingDialectical();
        Map<String, Object> isHaveAdmittingDialectical1 = getData0("isHaveAdmittingDialectical", isHaveAdmittingDialectical);
        datas.putAll(isHaveAdmittingDialectical1);

        datas.put("admittingDialecticalDetail", vo.getAdmittingDialecticalDetail());

        String isHavaUse = vo.getIsHavaUse();
        Map<String, Object> isHavaUse1 = getData0("isHavaUse", isHavaUse);
        datas.putAll(isHavaUse1);

        datas.put("useDays", vo.getUseDays());

        Date firstUseDate = vo.getFirstUseDate();
        int year = DateUtil.year(firstUseDate) - 2000;
        int month1 = DateUtil.month(firstUseDate) + 1;
        int day1 = DateUtil.dayOfMonth(firstUseDate);
        datas.put("firstUseDate0", year);
        datas.put("firstUseDate1", month1);
        datas.put("firstUseDate2", day1);

        Date lastUseDate = vo.getLastUseDate();
        int year2 = DateUtil.year(lastUseDate) - 2000;
        int month2 = DateUtil.month(lastUseDate) + 1;
        int day2 = DateUtil.dayOfMonth(lastUseDate);
        datas.put("lastUseDate0", year2);
        datas.put("lastUseDate1", month2);
        datas.put("lastUseDate2", day2);

        datas.put("menstruumType", vo.getMenstruumType());
        datas.put("givePillsPathway", vo.getGivePillsPathway());

        String isHaveBad = vo.getIsHaveBad();
        Map<String, Object> isHaveBad1 = getData0("isHaveBad", isHaveBad);
        datas.putAll(isHaveBad1);

        datas.put("givePillsPathway2", vo.getGivePillsPathway2());
        datas.put("pillsUsage", vo.getPillsUsage());
        List<Map<String, Object>> extendOneInfo = new ArrayList<>();
        int i = 1;
        for (ElectronicExtendOneInfoVo oneInfoVo : extendOneList) {
            Map<String, Object> ext = new HashMap<>();
            //ext.put("count", oneInfoVo.getCount());
            ext.put("count", i);
            Date beginTime = oneInfoVo.getBeginTime();
            int month = DateUtil.month(beginTime) + 1;
            int day = DateUtil.dayOfMonth(beginTime);
            int hour = DateUtil.hour(beginTime, true);
            int minute = DateUtil.minute(beginTime);
            ext.put("beginTime0", month);
            ext.put("beginTime1", day);
            ext.put("beginTime2", hour);
            ext.put("beginTime3", minute);
            ext.put("batchNo", oneInfoVo.getBatchNo());
            ext.put("placeTime", oneInfoVo.getPlaceTime());
            ext.put("frequency", oneInfoVo.getFrequency());
            ext.put("singleAmount", oneInfoVo.getSingleAmount());
            ext.put("menstruumType", oneInfoVo.getMenstruumType());
            ext.put("singleMenstruumAmount", oneInfoVo.getSingleMenstruumAmount());
            ext.put("giveIllnessPathway", oneInfoVo.getGiveIllnessPathway());
            ext.put("quietSpeed", oneInfoVo.getQuietSpeed());
            ext.put("quietContinueTime", oneInfoVo.getQuietContinueTime());
            extendOneInfo.add(ext);
            i++;
        }
        datas.put("extendOneInfo", extendOneInfo);
        List<Map<String, Object>> extendTwoInfo = new ArrayList<>();
        int j = 1;
        for (ElectronicExtendTwoInfoVo twoInfoVo : extendTwoList) {
            Map<String, Object> ext = new HashMap<>();
            //ext.put("serialNumber", twoInfoVo.getSerialNumber());
            ext.put("serialNumber", j);
            ext.put("pillsName", twoInfoVo.getPillsName());
            ext.put("givePillsPathway", twoInfoVo.getGivePillsPathway());
            ext.put("pillsUsage", twoInfoVo.getPillsUsage());
            ext.put("pillsAmount", twoInfoVo.getPillsAmount());
            ext.put("endTime", twoInfoVo.getEndTime());
            extendTwoInfo.add(ext);
            j++;
        }
        datas.put("extendTwoInfo", extendTwoInfo);

        datas.put("inspectorSign", vo.getInspectorSign());
        Date inspectorDate = vo.getInspectorDate();
        int year3 = DateUtil.year(inspectorDate) - 2000;
        int month3 = DateUtil.month(inspectorDate) + 1;
        int day3 = DateUtil.dayOfMonth(inspectorDate);

        datas.put("inspectorDate0", year3);
        datas.put("inspectorDate1", month3);
        datas.put("inspectorDate2", day3);
        datas.put("qualityControllerSign", vo.getQualityControllerSign());

        Date qualityControllerDate = vo.getQualityControllerDate();
        int year4 = DateUtil.year(qualityControllerDate) - 2000;
        int month4 = DateUtil.month(qualityControllerDate) + 1;
        int day4 = DateUtil.dayOfMonth(qualityControllerDate);
        datas.put("qualityControllerDate0", year4);
        datas.put("qualityControllerDate1", month4);
        datas.put("qualityControllerDate2", day4);
        return datas;
    }


    public Map<String, Object> getData0(String key, String value) {
        String[] list = {"0", "1", "2", "3"};
        Map<String, Object> datas = new HashMap<>();
        if (StringUtils.isEmpty(value)) {
            return datas;
        }
        for (String str : list) {
            String key1 = key + str;
            if (str.equals(value)) {
                datas.put(key1, new TextRenderData(getKey(85), new Style("Wingdings 2", 13)));
            } else {
                datas.put(key1, new TextRenderData(getKey(153), new Style("Wingdings 2", 13)));
            }
        }
        return datas;
    }

    public Map<String, Object> getData1(String key, String value) {
        String[] list = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"};
        Map<String, Object> datas = new HashMap<>();
        if (StringUtils.isEmpty(value)) {
            return datas;
        }
        if (!value.contains(",")) {
            for (String str : list) {
                String key1 = key + str;
                if (str.equals(value)) {
                    datas.put(key1, new TextRenderData(getKey(83), new Style("Wingdings 2", 13)));
                } else {
                    datas.put(key1, new TextRenderData(getKey(163), new Style("Wingdings 2", 13)));
                }
            }
        } else {
            String[] split = value.split(",");
            for (String str : split) {
                String key1 = key + str;
                //if (str.equals(value)) {
                datas.put(key1, new TextRenderData(getKey(83), new Style("Wingdings 2", 13)));
                //}
            }
            for (String str : list) {
                String key1 = key + str;
                if (datas.get(key1) == null) {
                    datas.put(key1, new TextRenderData(getKey(163), new Style("Wingdings 2", 13)));
                }
            }

        }
        return datas;
    }

    public Map<String, Object> getData2(String key, String value) {
        Map<String, Object> datas = new HashMap<>();
        if (value != null) {
            String[] split = value.split("；");
            for (int i = 0; i < split.length; i++) {
                String key1 = key + (i + 1);
                datas.put(key1, split[i].trim());
            }
        }
        return datas;
    }

    public static String getKey(int num) {
        char ch = (char) num;
        return String.valueOf(ch);
    }


}
